package com.fingard.dsp.bank.directbank.cqcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.directbank.cqcb01.CQCB01Header;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CQCB01Balance extends CQCB01Base implements IBalance {

    //单个余额查询和批量余额查询，统一采用流处理方式
    //1、循环调用Req7506BalQry.readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用RetAts7506BalQry.writeRetDetail()，写入返回ATS的明细
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {//余额查询
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
        try {
            //发送银行之前报文主体部分送到前置机的签名端口
            String bodycontent = "";
            String actNum = "";
            while (p_req7506.readRecord()) {
                actNum = p_req7506.readedDetail.actNum;
                bodycontent = "<body><trans><AccountNo>" + actNum + "</AccountNo></trans></body>";
                int length = bodycontent.getBytes().length;
                //	以下2行为签名前置服务器要求送的串，qmhead无需修改
                String qmhead = "http://192.168.0.133/servlet/CQBANK.Trans HTTP/1.0\r\nAccept:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint,application/vnd.ms-excel,application/msword, */*\r\nReferer: http://192.168.0.133/CQBANKB2E.htm\r\nAccept-Language: zh-cn\r\nContent-Type: INFOSEC_SIGN/1.0\r\nProxy-Connection: Keep-Alive\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\nHost: 192.168.0.133\r\n";
                String qmlength = "Content-Length:" + " " + String.valueOf(length) + "\r\n\r\n";
                //	构造的最终串
                String qm = qmhead + qmlength + bodycontent;
                String[] tmpSign = sendToBankSignXml(qm);
                if (tmpSign[0].length() == 0) {
                    String signature = "";
                    String body = "";
                    String aa = "<result>";
                    int i = tmpSign[1].indexOf(aa);
                    if (i != -1) {
                        body = tmpSign[1].substring(i + 8, i + 9);
                    }
                    if (body.equals("0")) {
                        String ab = "<sign>";
                        String ac = "</sign>";
                        int ib = tmpSign[1].indexOf(ab);
                        int ic = tmpSign[1].indexOf(ac);
                        //以下为最终的签名结果
                        signature = tmpSign[1].substring(ib + 6, ic);
                        //赋值通用参数
                        CQCB01Header bankHeader = new CQCB01Header();
                        bankHeader.transCode = "4402";
                        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                        //写入通用的头部节点
                        writeCommonHeader(xmlWriter, bankHeader);//发送银行的字符串
                        xmlWriter.writeElementString("signature", signature);
                        xmlWriter.writeEndElement();
                        xmlWriter.writeEndDocument();//写入报文完成
                        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                        if (tmpStrRet[0].length() == 0) {
                            StringBuilder tmpSbMsg = new StringBuilder();

                            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                            if (tmpBkReader != null) {
                                String tmpStatus = tmpBkReader.getFirstText("/out/head/ErrorCode");
                                String tmpStatusText = tmpBkReader.getFirstText("/out/head/ErrorMsg");
                                if (tmpStatus != null && tmpStatus.equals("0000")) {
                                    while (tmpBkReader.read("/out/body/Trans")) {
                                        tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AccountNo");
                                        tmpRetDetail.respCode = "0";
                                        tmpRetDetail.actCur = tmpBkReader.readedXElement.getSingleText("Currency");
                                        tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("UseableAmt");
                                        tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Amount");
                                        tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleText("FreezeAmt");
                                        tmpRetDetail.actArea = tmpBkReader.readedXElement.getSingleText("BranchNo");
                                    }
                                } else {
                                    tmpRetDetail.respCode = "-1";
                                    tmpRetDetail.respInfo = tmpStatusText;
                                }
                            } else {
                                tmpRetDetail.respCode = "-1";
                                tmpRetDetail.respInfo = tmpSbMsg.toString();
                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpStrRet[1];
                        }
                    } else {//签名数据返回数据为空
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSign[1];
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSign[1];
                }
                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }

}
